package cn.tedu.javagame.FirePlane;

import javax.swing.*;
import java.awt.*;

public class Bullet {
    int x, y;
    static Image bulletImg;

    //静态代码块，当类加载时（进入虚拟机）的时候会执行一次，无论以后创造多少个对象都不再执行
    static {
        bulletImg = new ImageIcon("plane/bullets.png").getImage();
    }

    public Bullet(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void move() {
        this.y -= 5; // 英雄机子弹向上移动
    }

    public boolean hitEnemy(Enemy e) {
        // 简单的碰撞检测：判断子弹的矩形区域是否与敌机的矩形区域相交
        Rectangle bulletRect = new Rectangle(x, y, bulletImg.getWidth(null), bulletImg.getHeight(null));
        Rectangle enemyRect = new Rectangle(e.x, e.y, e.enemyImg.getWidth(null), e.enemyImg.getHeight(null));
        return bulletRect.intersects(enemyRect);
    }

    public boolean hitHero(Hero h) {
        Rectangle heroRect = new Rectangle(h.x, h.y, h.heroImg.getWidth(null), h.heroImg.getHeight(null));
        Rectangle bulletRect = new Rectangle(x, y, bulletImg.getWidth(null), bulletImg.getHeight(null));
        return heroRect.intersects(bulletRect);
    }
}